// import { useToken } from '~/store/token'
import { getAdminInstitution, getUserInfoApi, loginOutApi, getCurrentAdminAuth } from '~/server/api'
import { isEmpty } from '~/utils/utils'
import { useUserinfo } from '~/store'

export default defineNuxtRouteMiddleware(async (to: any, from) => {
  const { userinfo, setUserinfo } = useUserinfo()
  // 视频APPID
  to.meta.video_appId = '1253774432';
  // 排除登录页
  if (to.path != '/login') {
    // 获取用户信息
    // to.meta.userinfo = null;
    // let { data: getuserRes }: any = await useAsyncData('getUserInfoApi', () => useServerRequest(getUserInfoApi, {
    //   method: 'get'
    // }))
    // if (getuserRes.value && getuserRes.value.errno === 0 && getuserRes.value.result) {
    //   to.meta.userinfo = getuserRes.value.result;
    // }
    to.meta.userinfo = userinfo || {};
    // 个人中心 判断用户信息是否为空
    if (/^\/userCenter(.*?)/.test(to.path) && isEmpty(to.meta.userinfo)) {
      // useToken().token = '';
      if (process.client) {
        useRouter().replace('/login')
      }
      return false
    }
    // 获取机构信息
    to.meta.mechanism = null;
    if (Object.keys(to.meta.userinfo).length > 0) {
      let { data: institutionRes }: any = await useAsyncData('getAdminInstitution', () => useServerRequest(getAdminInstitution, {
        method: 'get'
      }))
      if (institutionRes.value && institutionRes.value.errno === 0 && institutionRes.value.result) {
        to.meta.mechanism = institutionRes.value.result;
      }
    }
    // 机构管理 判断机构信息是否为空
    if (/^\/admin(.*?)/.test(to.path) && isEmpty(to.meta.mechanism)) {
      // useToken().token = '';
      if (process.client) {
        useRouter().replace('/login')
      }
      return false
    }
    // 获取机构导航数据
    to.meta.adminMenuData = null;
    if (/^\/admin(.*?)/.test(to.path) && !isEmpty(to.meta.mechanism)) {
      let { data: menuRes }: any = await useAsyncData(() => useServerRequest(getCurrentAdminAuth, {
        method: 'get'
      }))
      if (menuRes.value && menuRes.value.errno === 0 && menuRes.value.result) {
        to.meta.adminMenuData = menuRes.value.result;
      }
    }
    // 不等于题库中心模拟考试路径删除缓存
    if (!/^\/exam\/(.*?)\.html$/.test(to.path)) {
      if (process.client && localStorage.getItem('examId')) {
        localStorage.removeItem('examId')
      }
      if (process.client && localStorage.getItem('examAuth')) {
        let downTimeExamDelAuth = setTimeout(() => {
          localStorage.removeItem('examAuth')
          clearInterval(downTimeExamDelAuth)
        }, 1200)
      }
    }
    // 不等于题库中心练题路径删除缓存
    if (!/^\/exercises_details\/(.*?)\.html$/.test(to.path)) {
      if (process.client && localStorage.getItem('p')) {
        localStorage.removeItem('p')
      }
    }
  }
  // // 没有用户信息时删除Local storage and Cookies数据
  // if (isEmpty(to.meta.userinfo) && process.client) {
  //   if (localStorage.getItem('token')) {
  //     ElMessage.error('请重新登录')
  //     localStorage.removeItem('token')
  //   }
  //   var cookies = document.cookie.split("; ");
  //   for (var c = 0; c < cookies.length; c++) {
  //     var d = window.location.hostname.split(".");
  //     while (d.length > 0) {
  //       var cookieBase = encodeURIComponent(cookies[c].split(";")[0].split("=")[0]) + '=; expires=Thu, 1-Jan-1970 00:00:01 GMT; domain=' + d.join('.') + ' ;path=';
  //       var p = location.pathname.split('/');
  //       document.cookie = cookieBase + '/';
  //       var domain = '.' + p[1] + '.' + p[2];
  //       document.cookie = cookieBase + domain;
  //       d.shift();
  //     }
  //   }
  // }
})
